package o2o.web.actions.pay;


//检测支付线程

import com.jinyou.utils.common.DateUtils;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.order.OrderInfo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PayThreads extends Thread {
    public static Map<String, OrderInfo> map = new ConcurrentHashMap<String, OrderInfo>();   //支付状态待检测map

    private boolean threadFlag = true;

    private static PayThreads thread = new PayThreads();

    public static PayThreads getInstance() {
        if (thread == null) {
            thread = new PayThreads();
        }
        return thread;
    }

    private PayThreads() {
    }


    @Override
    public void run() {
        while (threadFlag) {
            if (map.size() > 0) {
//                SMSService service = DubboService.getInstance().getSmsService();
                synchronized (map) {
                    //检测是否已支付成功
                    for (String key : map.keySet()) {
//                        service.loginByTelcode(str, map.get(str), Common.projectName);   //发送手机验证码   验证码登录
//                        map.remove(str);

                        OrderInfo orderInfo = map.get(key);
                        if (null == orderInfo) {
                            map.remove(key);
                            continue;
                        }

                        //已经支付成功的就不惜要查了
                        if (0 == 1 - orderInfo.getIsPaid()) {
                            map.remove(key);
                            continue;
                        }

                        //支付时间错过30分钟的就不查了
                        if (orderInfo.getPayTime() - DateUtils.getCurrTime() > 30 * 60 * 1000) {
                            map.remove(key);
                            continue;
                        }

                        ResultInfo resultInfo = OrderPay.userOrderPayOver(orderInfo.getSysAppKey(), orderInfo.getPayTypeFlag(), orderInfo.getPayType(), key, orderInfo.getChargeId());
                        if (null == resultInfo) {
                            continue;
                        } else {
                            if (!resultInfo.isSuccess()) {//失败，返回错误
                                continue;
                            }
                        }

                        map.remove(key);
                    }
                }
            }

            try {
                Thread.sleep(20000);  //线程每次停止20秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
